// ==UserScript==
// @name         JS推送题目
// @namespace    http://tampermonkey.net/
// @version      0.1.20220516
// @description  仅用于学习使用，网课自动挂机看视频，网课作业自动做题共享答案。网课在线辅助脚本❤网课辅助题库❤全自动化操作挂机【易全题库】
// @author       YanMing
// @match        *://www.baidu.com/*
// @match        *://*.uooconline.com/*
// @icon         
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const SETTING = {
        api: "http://localhost:8080"
    }
    uooc_course_list();
    //uooc_course_id_catalog('1091609371')
    // 获取课程列表
    function uooc_course_list(){
        // 已学完的课程https://cce.org.uooconline.com/home/course/list?keyword=&page=1&type=finish
        setTimeout(() => {
            uooc_course_learn_type('finish')
        }, 1000);
        // 正在学的课程https://cce.org.uooconline.com/home/course/list?keyword=&page=1&type=learn
        // 延后执行
        setTimeout(() => {
            uooc_course_learn_type('learn')
        }, 3000);

    }

    let  pushCourseInfo = {course_id:'',course_name:'',org_name:'',teacher_name:'',chapter_id:'',chapter_name:'',section_id:'',subsection_id:'',subsection_name:'',catalog_id:'',task_id:''}
    function uooc_course_learn_type(type){
        if(type == "learn" || type == "finish" ){
            $.ajax({
                type: "GET",
                url: '/home/course/list',
                data: {
                    page: 1,
                    type: type,
                    keyword: ''
                },
                success: function (response) {
                    // 判断是否提交试卷
                    if (response.code == 1) {
                        if(response.data && response.data.data && response.data.data.length > 0) {
                            uooc_course(response.data.data)
                        }
                    }
                }
            });
        }

    }

    function uooc_course(courseData){
        if(courseData && courseData.length>0){
            for(let i=0;i<courseData.length;i++){
                let courseInfo = JSON.parse(JSON.stringify(pushCourseInfo))
                courseInfo.course_id = courseData[i].id;
                courseInfo.course_name = courseData[i].parent_name;
                courseInfo.org_name = courseData[i].org_name;
                courseInfo.teacher_name = courseData[i].uname;
                const course = {course_id: courseData[i].id,course_name: courseData[i].parent_name,org_name: courseData[i].org_name,teacher_name: courseData[i].uname}
                // 获取章节目录
                uooc_course_catalog(courseInfo);
                // 发送课程信息到服务器
                $.ajax({
                    type: "POST",
                    url: SETTING.api+'/uooc/course',
                    dateType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(course),
                    success: function (response) {

                    }
                });

            }
        }
    }

    function uooc_course_catalog(course){
        var courseInfo = JSON.parse(JSON.stringify(course))
        $.ajax({
            type: "GET",
            url: '/home/learn/getCatalogList',
            data: {
                cid: courseInfo.course_id
            },
            success: function (response) {
                if (response.code == 1) {
                    // 发送课程章节信息到服务器
                    let data = JSON.stringify({courseId:courseInfo.course_id,courseCatalogList: response.data});
                    $.ajax({
                        type: "POST",
                        url: SETTING.api+'/uooc/course/catalog',
                        dateType: "json",
                        contentType: "application/json",
                        data: data,
                        success: function (response) {

                        }
                    });
                    // 遍历课程章节信息
                    uooc_course_catalog_loop(courseInfo, response.data)
                }
            }
        });
    }

    function uooc_course_catalog_loop(course, data){
        let courseInfo = JSON.parse(JSON.stringify(course))
        for(const chapter of data){
            // 章节 (先清空上次的数据)
            courseInfo.chapter_id = chapter.id;
            courseInfo.chapter_name = chapter.name

            if (typeof chapter.children != 'undefined') {
                // 目录（二级目录）
                for(const section of chapter.children){
                    courseInfo.section_id = section.id
                    courseInfo.section_name = section.name
                    if (typeof section.children != 'undefined') {
                        // 目录（三级级目录）
                        for(var subsection of section.children){
                            courseInfo.subsection_id = subsection.id
                            courseInfo.subsection_name = subsection.name
                            if (typeof subsection.children != 'undefined') {
                                console.warn("&&&&&&还有目录,暂不支持&&&&&&");
                            }else if(subsection.task_id){
                                //console.log("subsection测验"+chapter.name +" "+section.name +" "+subsection.name+"("+subsection.id+")");
                                courseInfo.task_id = subsection.task_id
                                if( subsection.finished == 1 ){
                                    // 已完成的作业进行同步
                                    uooc_course_chapter_task_exam (courseInfo);
                                }
                            }else{
                                courseInfo.catalog_id = subsection.id
                                uooc_course_chapter_section(courseInfo);
                            }
                        }
                    }else if(section.task_id){
                        courseInfo.subsection_id =  ''
                        courseInfo.subsection_name =  ''
                        courseInfo.catalog_id =  section.id
                        courseInfo.task_id = section.task_id
                        if( section.finished == 1 ){
                            // 已完成的作业进行同步
                            uooc_course_chapter_task_exam (courseInfo)
                        }
                    }else{
                        courseInfo.subsection_id =  ''
                        courseInfo.subsection_name =  ''
                        courseInfo.task_id =  ''
                        courseInfo.catalog_id = section.id
                        uooc_course_chapter_section(courseInfo);
                    }
                }
            }else if(chapter.task_id){
                //console.log("chapter测验"+chapter.name+"("+chapter.id+")");
                courseInfo.section_id = ''
                courseInfo.section_name =  ''
                courseInfo.subsection_id =  ''
                courseInfo.subsection_name =  ''
                courseInfo.catalog_id =  ''
                courseInfo.task_id = chapter.task_id
                if( chapter.finished == 1 ){
                    // 已完成的作业进行同步
                    uooc_course_chapter_task_exam(courseInfo)
                }
            }else{
                courseInfo.section_id = ''
                courseInfo.section_name =  ''
                courseInfo.subsection_id =  ''
                courseInfo.subsection_name =  ''
                courseInfo.task_id =  ''
                courseInfo.catalog_id = chapter.id
                uooc_course_chapter_section(courseInfo);
            }
        }

    }

    function uooc_course_chapter_section(course){
        var courseInfo = JSON.parse(JSON.stringify(course))
        if(courseInfo && courseInfo.course_id){
            $.ajax({
                type: "GET",
                url: '/home/learn/getUnitLearn',
                data: {
                    cid: courseInfo.course_id,//课程ID
                    chapter_id: courseInfo.chapter_id,//章节ID(一级目录ID)
                    section_id: courseInfo.section_id,//内容部分ID(二级目录ID)
                    subsection_id: courseInfo.subsection_id,//内容部分ID(三级目录ID)
                    catalog_id: courseInfo.catalog_id,//当前目录ID
                },
                success: function (response) {
                     if (response.code == 1) {
                        const data =response.data;
                        for (let l = 0; l < data.length; l++) {
                            if(data[l].task_id){
                                courseInfo.task_id = data[l].task_id
                                if( data[l].finished == 1){
                                    // 已完成的作业进行同步
                                    uooc_course_chapter_task_exam(courseInfo);
                                }
                            }
                        }
                    }

                }
            })
        }

    }


    function uooc_course_chapter_task_exam(course){
        var courseExamInfo = JSON.parse(JSON.stringify(course))
        $.ajax({
            type: "GET",
            url: '/exam/view',
            data: {
                cid: courseExamInfo.course_id,
                tid: courseExamInfo.task_id
            },
            success: function (response) {
               if (response.code == 1) {
                   uooc_push_course_chapter_task_exam_questions (courseExamInfo,response.data)
                }
            }
        });

    }

    function uooc_push_course_chapter_task_exam_questions(course, data) {
        const courseInfo = JSON.parse(JSON.stringify(course))
        console.log("测验==>"+courseInfo.course_name+"("+courseInfo.course_id+")->"+courseInfo.chapter_name+"("+courseInfo.chapter_id+")->"+courseInfo.section_name+"("+courseInfo.section_id+")->"+courseInfo.subsection_name+"("+courseInfo.subsection_id+")-> Task = "+courseInfo.task_id)
        // 提交试卷到服务器
        const examPushData = JSON.stringify( {course:courseInfo, exam:data});
        $.ajax({
            type: "POST",
            url: SETTING.api+'/uooc/exam/push',
            dateType: "json",
            contentType: "application/json",
            data: examPushData,
            success: function (response) {

            }
        });
    }


})();

